package 多线程.多线程同步的5种方法;

import java.util.Date;

/**
 * @date 2017/8/31.
 * @author 廖述京
 */
public class ThreadLocal_Bank {
    //private int count =0;//账户余额

    //将count定义为一个ThreadLocal管理的本地局部变量
    private ThreadLocal<Integer> count=new ThreadLocal<Integer>(){
        @Override
        protected Integer initialValue() {
            return 0;
        }
    };

    //存钱
    public  void addMoney(int money){
//        count +=money;
        count.set(count.get()+money);
        System.out.println(new Date(System.currentTimeMillis()).toLocaleString()+"存进："+money);
    }

    //取钱
    public  void subMoney(int money){
        if(count.get()-money < 0){
            System.out.println("余额不足");
            return;
        }
//        count -=money;
        count.set(count.get()-money);
        System.out.println(new Date(System.currentTimeMillis()).toLocaleString()+"取出："+money);
    }

    //查询
    public void lookMoney(){
        System.out.println("账户余额："+count.get());
    }
}
